package com.rongshu.sendmessage.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.base.BaseActivity;
import com.rongshu.sendmessage.base.MyApplication;
import com.rongshu.sendmessage.util.ClientPublicParamUtil;
import com.rongshu.sendmessage.util.DialogUtil;
import com.rongshu.sendmessage.util.ToastUtils;
import com.rongshu.sendmessage.util.httpUtil.HYContent;
import com.rongshu.sendmessage.util.httpUtil.IModel;

import java.util.HashMap;
import java.util.Map;

public class YiJianActivity extends BaseActivity {

    private EditText et_shijian;
    private TextView tv_shijian_tongji;
    private String userId;
    private int selectIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yi_jian);
        mContext=this;
        initView();
        userId= MyApplication.mSettings.getString(HYContent.UserId,"");
    }

    private void initView() {
        et_shijian=findViewById(R.id.et_des);
        TextView title=findViewById(R.id.title);
        title.setText("意见反馈");
        LinearLayout titleLayLeft = findViewById(R.id.titleLayLeft);
        titleLayLeft.setOnClickListener(view->finish());
        tv_shijian_tongji=findViewById(R.id.tv_zishu);
        et_shijian.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable s) {
                try{
                    if(s!=null && !"".equals(s.toString())) {
                        setButton();
                        if (s.toString().length()>200){
                            et_shijian.setText(s.toString().substring(0,200));
                            et_shijian.setSelection(200);
                            tv_shijian_tongji.setText("200");
                            Toast.makeText(getApplicationContext(), "最多输入200个字符", Toast.LENGTH_SHORT).show();
                        }else{
                            tv_shijian_tongji.setText(s.toString().length()+"");
                        }
                    }else{
                        tv_shijian_tongji.setText("0");
                        setButton();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
    private boolean canSubmit;
    private void setButton() {
        if (et_shijian.length()<10){
            canSubmit=false;
        }else{
            canSubmit=true;
        }

    }

    public void onClick(View view) {
        if (view.getId()==R.id.btn_submit){
            setButton();
            if (!canSubmit){
                ToastUtils.show(mContext,"描述信息需要大于10个字哦");
                return;
            }
            submit();
        }

    }

    private void submit() {
        DialogUtil.showProgress(mContext);
        String str="";
        switch (selectIndex){
            case 1:
                str="产品不好用";
                break;
            case 2:
                str="页面不好用";
                break;
            case 3:
                str="功能有bug";
                break;
            case 4:
                str="其他";
                break;
        }
        Map<String, String> param = new HashMap<String, String>();
        param.put("userId",userId);
        param.put("title",str);
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        param.put("content",et_shijian.getText().toString());
        OkGo.<String>post(new HYContent().YIJIAN_URL).params(param).tag("YIJIAN_URL").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                IModel.AjaxResult jr = new IModel.AjaxResult(response.body(),false);
                Log.d("user",response.body());
                final int code = jr.getCode();
                if(code > 0) {


                    finish();
                }
                ToastUtils.show(mContext,jr.getMsg());
                DialogUtil.hideProgress();
            }
            @Override
            public void onError(Response<String> response) {
                ToastUtils.show(mContext,"操作失败");
                DialogUtil.hideProgress();
            }
        });

    }

}